﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;

namespace Gama.Extension
{
    static class Helpers
    {
        internal static ViewDataDictionary BuildViewDataDictionaryFromProperties(object @object)
        {
            var dictionary = new ViewDataDictionary();

            if (@object != null)
            {
                foreach (var propertyDescriptor in TypeDescriptor.GetProperties(@object).Cast<PropertyDescriptor>())
                {
                    dictionary.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(@object));
                }
            }

            return dictionary;
        }

        internal static IDictionary<string, object> BuildDictionaryFromProperties(object @object)
        {
            var dictionary = new Dictionary<string, object>();

            if (@object != null)
            {
                foreach (var propertyDescriptor in TypeDescriptor.GetProperties(@object).Cast<PropertyDescriptor>())
                {
                    dictionary.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(@object));
                }
            }

            return dictionary;
        }
    }
}
